﻿using System;
using nRoute.Components.Composition;
using nRoute.Utilities;
using nRoute.ViewModels;

namespace nRoute.Mapping
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
    public abstract class MapViewViewModelBaseAttribute
		 : MapResourceBaseAttribute
    {
        private Type _viewType;
        private Type _viewModelType;

        public MapViewViewModelBaseAttribute() { }

        public MapViewViewModelBaseAttribute(Type viewType) : this()
        {
            Guard.ArgumentNotNull(viewType, "viewType");
            this.ViewType = viewType;
        }

        public MapViewViewModelBaseAttribute(Type viewType, Type viewModelType) : this(viewType)
        {
            Guard.ArgumentNotNull(viewModelType, "viewModelType");
            this.ViewModelType = viewModelType;
        }

#region Properties

        public Type ViewType
        {
            get { return _viewType; }
            protected set
            {
                base.Key = value != null ? value.FullName : null;
                _viewType = value;
            }
        }

        public Type ViewModelType
        {
            get { return _viewModelType; }
            protected set
            {
                _viewModelType = value;
            }
        }

#endregion

#region Overrides


        protected internal override Type GetResourceType(Type targetType)
        {
            return typeof(IViewModelProvider);
        }

        protected internal override IResourceLocator GetResourceLocator(Type targetType, ITypeBuilder typeBuilder)
        {
            return new DefaultViewModelLocator(this.ViewType, this.ViewModelType);
        }

#endregion

    }

}